package com.minelx.serverandclient.stream.divider;

import com.minelx.serverandclient.common.Bytes;
import com.minelx.serverandclient.stream.divider.Divider;

import java.io.IOException;
import java.io.InputStream;

public class StreamDivider implements Divider {

	private final InputStream source;

	private final Bytes bytes;

	// FIXME 2020/11/18  wait for me!!!   PROBABLY combine with BytesConverter
	StreamDivider(InputStream source, int lengthOfAnItem) {
		this.source = source;

		bytes = new Bytes(new byte[lengthOfAnItem]);
	}

	@Override
	public byte[] next() {
		System.out.println("hello");
		try {
			bytes.absorb(source);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bytes.getBytes();
	}

	@Override
	public boolean hasNext() {
		try {
			return source.available() != 0;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public int indexedCount() {
		return bytes.absorbedCount();
	}
}
